﻿using System.Web.Mvc;
using QuanLyCuaHang.Models;
using System.Linq;
using System;
using System.Collections.Generic;
using QuanLyCuaHang.Models.Display;

namespace QuanLyCuaHang.Controllers
{
    public class HomeController : Controller
    {
        QuanLy db = new QuanLy();
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Regions()
        {
            var Khuvuc = db.KhuVucs.ToList();
            var Cuahang = db.CuaHangs.ToList();
            ViewBag.Message = "Vùng vị trí!";
            var tuple = new Tuple<List<KhuVuc>,List<CuaHang>>(Khuvuc, Cuahang);
            return View(tuple);
        }
        //[HttpPost]
        //public ActionResult AddStore(string name)
        //{
        //    string temp = "Chào " + name + " bây giờ là " + DateTime.Now.Hour.ToString() + " giờ" + Server.HtmlDecode("&copy;");
        //    return Json(temp);
        //}
        public ActionResult Opacity()
        {
            return View();
        }
        public ActionResult AddStore()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddStore(Store store)
        {
            CuaHang ch =  new CuaHang{
                TenCuaHang = store.TenCuaHang,
                MieuTa = store.MieuTa,
                LoaiKinhDoanh = store.LoaiKinhDoanh,
                LoaiCuaHang = store.LoaiCuaHang,
                DiaChi = store.DiaChi,
                ChuCuaHang = store.ChuCuaHang,
                KhuVucId = 1
            };
            if (ModelState.IsValid)
            {
                db.CuaHangs.Add(ch);                   
                db.SaveChanges();
                var id = ch.CuaHangId;

                return Json(id);
            }
            return Json("Error");
        }
        
        [HttpPost]
        public ActionResult ChangeRegions(string str)
        {
            string[] arr = str.Split(';');

            string check = "";

            /// Kiểm tra xâu hợp lệ
            /// 

            if (
               (check = checkString(str)) != ""
            )
            {
                return Json(check);
            }

            /// Kiểm tra vùng Id
            /// 
            if (
               (check = checkRegionId(str)) != ""
            )
            {
                return Json(check);
            }

            /// Kiểm tra cửa hàng Id khác nhau từng đôi một
            /// 
            if (
               (check = checkStoreId(str)) != ""
            )
            {
                return Json(check);
            }

            return Json(arr[0]);
        }
        public ActionResult AddRegion()
        {
            return View();
        }



        private string checkStoreId(string str)
        {
            throw new NotImplementedException();
        }

        private string checkRegionId(string str)
        {
            throw new NotImplementedException();
        }

        private string checkString(string str)
        {

            throw new NotImplementedException();
        }
    }
}
